<?php
/**
 * 
 * @author Youssef Driouach
 *
 */
class Application_Model_RentMapper{

	protected $_dbTable;

	public function setDbTable($dbTable)
	{
		if (is_string($dbTable)) {
			$dbTable = new $dbTable();
		}
		if (!$dbTable instanceof Zend_Db_Table_Abstract) {
			throw new Exception('Invalid table data gateway provided');
		}
		$this->_dbTable = $dbTable;
		return $this;
	}

	public function getDbTable()
	{
		if (null === $this->_dbTable) {
			$this->setDbTable('Application_Model_DbTable_Rent');
		}
		return $this->_dbTable;
	}
	
	public function save(Application_Model_Rent $rent)
	{
		$offerMapper = new Application_Model_OfferMapper();
		$row = $offerMapper->save($rent);
	
		$offer = new Application_Model_Offer();
		//TODO how to identifie the Offer
		//$where = array('id_object = ?' => (int)$offer->getObject()->getObjectId());
		$offer->setOfferId($row);
	
		$data = array(
				'id_offer' 	=> $offer->getOfferId(),
				'period'	=> $rent->getPeriod(),
		);
	
		if (null === ($id = $rent->getRentId())) {
			unset($data['id_rent']);
			return $this->getDbTable()->insert($data);
		} else {
			$this->getDbTable()->update($data, array('id_rent = ?' => $id));
			return $id;
		}
			
	}
	
	public function find($id, Application_Model_Rent $rent)
	{
		$result = $this->getDbTable()->find($id);
		if (0 == count($result)) {
			return;
		}
		$row = $result->current();
		$rent->setRentId($row->id_rent);
		$rent->setOfferId($row->id_offer);
		$rent->setPeriod($row->period);
		$offerMapper = new Application_Model_OfferMapper();
		$rent = $offerMapper->find($row->id_offer, $rent);
	
		
		return $rent;
	}
	
	public function findWhere($where)
	{
		$row = $this->getDbTable()->fetchRow($where);
		if (0 == count($row)) {
			return null;
		}
		
		$offerMapper = new Application_Model_OfferMapper();
		$offer = $offerMapper->find($row->id_offer, new Application_Model_Offer());
		
		$rent = new Application_Model_Rent();
		$rent->setRentId($row->id_rent);
		$rent->setPeriod($row->period);
		
		return $rent;
	}
	
	public function delete($id)
	{
		$rent = new Application_Model_Rent();
		$offerMapper = new Application_Model_OfferMapper();
		$rent = $this->find($id, new Application_Model_Rent());
	
		$result = $this->getDbTable()->find($id);
		if (0 != count($result)) {
			$this->getDbTable()->delete(array('id_rent = ?' => $id));
		}
	
	
		$offerMapper->delete($rent->getOfferId());
	
	}
	
	public function deleteOfferOnly($id){
		
		$rent = new Application_Model_Rent();
		$offerMapper = new Application_Model_OfferMapper();
		$rent = $this->find($id, new Application_Model_Rent());
		
		$result = $this->getDbTable()->find($id);
		if (0 != count($result)) {
			$this->getDbTable()->delete(array('id_rent = ?' => $id));
		}
		
		$offerMapper->deleteOfferOnly($rent->getOfferId());
	}
	
	public function indexBuilder(Zend_Search_Lucene_Document $doc, Application_Model_Rent $rent)
	{
		$offerMapper = new Application_Model_OfferMapper();
		$doc->addField(Zend_Search_Lucene_Field::Text('period', $rent->getPeriod(), 'utf-8'));
		$doc->addField(Zend_Search_Lucene_Field::Text('offerClass', 'rent', 'utf-8'));
		$doc = $offerMapper->indexBuilder($doc, $rent);
	
		return $doc;
	}
}	